#include "Menu_main.h"

Menu_main::Menu_main(void)
{
	Db_manager::list();
	submenu = 0;
}


Menu_main::~Menu_main(void)
{

}

void Menu_main::start()
{
	int chosen = 0;

	do
	{
		switch(chosen)
		{
		case 1:
			{
				db_load(true);
				break;
			}
		case 2:
			{
				db_list();
				break;
			}
		case 3:
			{
				db_load(false);
				break;
			}
		}

		cout << "### Database manager ###" << endl << "### Main menu ###" << endl;
		cout << "1. Create db" << endl;
		cout << "2. List dbs" << endl;
		cout << "3. Load db" << endl;
		cout << "4. Exit" << endl;
		chosen = Validator::get<int>("Please choose a number: ");
		system("cls");
	} while(chosen != 4);
}

void Menu_main::db_load(const bool &newdb)
{
	string name = Db_manager::load(newdb);
	if(name != "")
	{
		submenu = new Menu_db(name);
		submenu->start();
		delete submenu;
		Db_manager::list();
	}
}

void Menu_main::db_list() const
{
	vector<string> dbnames = Db_manager::get_dbnames();
	cout << "### Db list ###" << endl;
	for(vector<string>::const_iterator it = dbnames.begin(); it != dbnames.end(); ++it)
		cout << *it << endl;
	cout << endl;
}